/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.qc.bdeb.info.controleur;

import java.sql.*;

/**
 *
 * @author Usager
 */
public class ConnexionBD
{
    public Connection con;

    public ConnexionBD() throws Exception
    {
        con = null;
    }

    public void deconnecter() throws Exception
    {
        try
        {
            con.close();
        }
        catch ( Exception e )
        {
            System.err.print(e);
        }
    }

    public ResultSet queryExec( String query ) throws Exception
    {
        final String URL = "jdbc:mysql://127.0.0.1:3306/";
        final String DB_NAME = "jeu";
        final String DRIVER = "com.mysql.jdbc.Driver";
        final String USER_NAME = "root";
        final String PASSWORD = "root";
        
        Statement stmt = null;
        ResultSet set = null;

        try
        {
            Class.forName(DRIVER);
            con = DriverManager.getConnection(URL + DB_NAME, USER_NAME, PASSWORD);

            try
            {
                stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
                set = stmt.executeQuery(query);

                return set;
            }
            catch ( Exception e )
            {
                System.err.print(e);
                deconnecter();
                return set;
            }
        }
        catch (ClassNotFoundException e)
        {
            System.err.println("\n\nErreur 1 lors du chargement du pilote : " + e);
            return set;
        }
        catch (SQLException sqle)
        {
            System.err.print("\n\nErreur 2 type SQL : " + sqle);
            return set;
        }

    }

    public void queryUpdate( String query ) throws Exception
    {
        Statement stmt = null;

        try
        {
            stmt = con.createStatement();
            stmt.executeUpdate(query);
        }
        catch ( Exception e )
        {
            System.err.print(e);
        }
        finally
        {
            stmt.close();
        }
    }

    /*

            while (rset.next())
            {
                System.out.println(rset.getInt(1));
                System.out.println(rset.getString("description"));
                System.out.println(rset.getString(3));
                System.out.println(rset.getString(4));
                System.out.println(rset.getString(5));
            }

*/
}
